package one.stand.controller;

import com.alibaba.fastjson.JSON;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.ShowroomLoginImgRequest;
import com.jmp.base.dto.ShowroomLoginImgResponse;
import com.jmp.base.dto.ShowroomLoginRequest;
import com.jmp.base.dto.ShowroomLoginResponse;
import com.jmp.feign.SecurityFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.LoginImgRequest;
import one.stand.dto.LoginImgResponse;
import one.stand.dto.LoginRequest;
import one.stand.dto.LoginResponse;
import one.stand.enums.LoginEnum;
import one.stand.model.ResultModel;
import one.stand.service.CompanyEquService;
import one.stand.service.user.TokenService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cjr
 */
@Api(description = "登录登出")
@RestController
@Slf4j
public class LoginController {

    protected final static String DEFAULT_VIDEO = "http://storage.hongmushichang.com/temp/201904191402224298380.mp4";

    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    private CompanyEquService companyEquService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("登录Code")
    @PostMapping("/login/img")
    public ResultModel<LoginImgResponse> loginImg(@RequestBody LoginImgRequest request) {
        log.info("request: {}", JSON.toJSONString(request));
        ShowroomLoginImgRequest loginImgRequest = ShowroomLoginImgRequest.builder()
                .equCode(request.getEquCode())
                .loginEnum(request.getType() == 0 ? LoginEnum.SHOWROOM_ANDROID : LoginEnum.SHOWROOM_WINDOWS)
                .build();
        ResultModel<ShowroomLoginImgResponse> result = securityFeign.showroomLoginImg(loginImgRequest);

        AssertUtil.isTrue(result.isSuccess(), result.getMessage(), ResultEnum.PARAM_CHECK.getCode());

        LoginImgResponse response = LoginImgResponse.builder()
                .codeUrl(result.getData().getCodeUrl())
                .token(result.getData().getToken())
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultModel<LoginResponse> login(@RequestBody LoginRequest request) {
        log.info("request: {}", JSON.toJSONString(request));

        ShowroomLoginRequest loginRequest = ShowroomLoginRequest.builder()
                .code(request.getCode())
                .equCode(request.getEquCode())
                .loginEnum(request.getType() == 0 ? LoginEnum.SHOWROOM_ANDROID : LoginEnum.SHOWROOM_WINDOWS)
                .build();
        ResultModel<ShowroomLoginResponse> result = securityFeign.showroomLogin(loginRequest);

        AssertUtil.isTrue(result.isSuccess(), result.getMessage(), ResultEnum.IPAD_FAIL_LOGIN.getCode());

//        String wxCode = businessCardBaseService.getWxTwoCode(result.getData().getCardId());
//
//        BusinessCardModel businessCardModel = businessCardBaseService.get(result.getData().getCardId());
//
//        CompanyShowroom companyShowroom = companyShowroomService.getByCompanyId(businessCardModel.getCompanyId());
//
//        List<CompanyBanner> companyBanners = companyBannerService.getByCompanyId(businessCardModel.getCompanyId(), CompanyBannerService.BANNER_TYPE_SHOWROOM);
//        List<LoginResponse.Banner> banners = ConvertUtils.list(companyBanners, companyBanner -> LoginResponse.Banner.builder()
//                .source(companyBanner.getSource())
//                .type(companyBanner.getType())
//                .event(String.valueOf(companyBanner.getEvent()))
//                .id(companyBanner.getEventId())
//                .build());

        LoginResponse loginResponse = LoginResponse.builder()
                .accessToken(result.getData().getToken())
//                .name(result.getData().getUserName())
//                .icon(result.getData().getAvatarUrl())
//                .wxCode(wxCode)
//                .position(businessCardModel.getPosition())
//                .banners(banners)
                .build();
//
//        if (Objects.nonNull(companyShowroom)) {
//            loginResponse.setVideoType(0);
//            if (CompanyShowroomService.SHOW_VIDEO_ON == companyShowroom.getShowVideo()) {
//                loginResponse.setVideoUrl(companyShowroom.getVideo());
//            } else if (StringUtils.isNoneBlank(companyShowroom.getVrUrl())) {
//                loginResponse.setVideoType(1);
//                loginResponse.setVideoUrl(companyShowroom.getVrUrl());
//            } else {
//                loginResponse.setVideoUrl(DEFAULT_VIDEO);
//            }
//            loginResponse.setVrUrl(companyShowroom.getVrUrl());
//        }

        return ResultModel.success(loginResponse);
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    @SecurityAuthCheck(company = true)
    public ResultModel logout() {
        final String equCode = RequestContextHolder.getRequestContext().getRequest().getEquCode();
        final String loginEnumCode = RequestContextHolder.getRequestContext().getUser().getLoginProgramName();
        LoginEnum loginEnum = LoginEnum.getByCode(loginEnumCode);
        final int type = LoginEnum.SHOWROOM_ANDROID.equals(loginEnum) ? CompanyEquService.TYPE_ANDROID : CompanyEquService.TYPE_WINDOWS;
        companyEquService.updateLastUserId(equCode, type, null);

        tokenService.remove(RequestContextHolder.getRequestContext().getRequest().getToken());

        return ResultModel.success();
    }
}
